package com.timothy.millionaire;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.*;
import android.widget.AdapterView.OnItemSelectedListener;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import com.timothy.calculate.CalculatorUtil;
import com.timothy.calculate.NumberUtil;

public class Calculate3Activity extends BaseActivity {
	private static final String LOG_TAG = "HomeActivity";
	
	Context mContext;

    Spinner spinner1;
    ArrayAdapter adapter1;
    String[] years;
    int yearTarget;
    EditText cus_YearTarget;

    double leastInvest = 10000;
    TextView investRangeTV;

    Spinner spinner2;
    ArrayAdapter adapter2;
    String[] invests;
    double yearInvest = 0.0;
    EditText cus_yearInvest;

    TextView resultMsg;
    Button calculate;

    TableRow adRow;
    AdView adView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        //hide title bar
//        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.calculate3);

        mContext = getApplicationContext();

        spinner1 = (Spinner) findViewById(R.id.spinner1);
        years = this.getResources().getStringArray(R.array.year_target_array);
        adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, years);
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner1.setAdapter(adapter1);
        spinner1.setOnItemSelectedListener(new SpinnerSelectedListener1());
        spinner1.setVisibility(View.VISIBLE);

        cus_YearTarget = (EditText) findViewById(R.id.cus_targetYear);

        investRangeTV = (TextView) this.findViewById(R.id.investRange);
        investRangeTV.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                double investMoney = Double.valueOf(investRangeTV.getText().toString());
                if(investMoney<leastInvest || investMoney> CalculatorUtil.MAX_YEAR_INVEST){

                }
            }
        });

        spinner2 = (Spinner) findViewById(R.id.spinner2);
        invests = this.getResources().getStringArray(R.array.year_invest_array);
        adapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, invests);
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner2.setAdapter(adapter2);
        spinner2.setOnItemSelectedListener(new SpinnerSelectedListener2());
        spinner2.setVisibility(View.VISIBLE);

        cus_yearInvest = (EditText) findViewById(R.id.cus_YearInvest);

        resultMsg = (TextView) this.findViewById(R.id.result);

        calculate = (Button) this.findViewById(R.id.calculate);
        calculate.setOnClickListener(calculateListener);

        adRow = (TableRow) this.findViewById(R.id.adRow);
        adView = new AdView(this, AdSize.BANNER, "a151b693eb6195b");
        adRow.addView(adView);
        adView.loadAd(new AdRequest());

	}
	
	class SpinnerSelectedListener1 implements OnItemSelectedListener{
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
			Log.i(LOG_TAG,"SpinnerSelectedListener1.onItemSelected()...");
			Log.i(LOG_TAG,"arg2="+String.valueOf(arg2));
            if(arg2 == 10){
                cus_YearTarget.setVisibility(View.VISIBLE);
                leastInvest = 100000;
                investRangeTV.setText("Year Invest("+ NumberUtil.formatData2Deci(leastInvest)+" - "+NumberUtil.formatData2Deci(CalculatorUtil.MAX_YEAR_INVEST)+")");
                investRangeTV.setTextColor(Color.RED);

            }else{
                cus_YearTarget.setVisibility(View.INVISIBLE);
                yearTarget = Integer.valueOf(years[arg2]);
                leastInvest = CalculatorUtil.getLeastInvestByYear(yearTarget);
                investRangeTV.setText("Year Invest("+ NumberUtil.formatData2Deci(leastInvest)+" - "+NumberUtil.formatData2Deci(CalculatorUtil.MAX_YEAR_INVEST)+")");
                investRangeTV.setTextColor(Color.RED);
                if(leastInvest>100000){
                    spinner2.setSelection(10);
                }
            }
		}
		public void onNothingSelected(AdapterView<?> arg0) {
			Log.i(LOG_TAG,"onNothingSelected()...");
		}
	}

	class SpinnerSelectedListener2 implements OnItemSelectedListener{
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
			Log.i(LOG_TAG,"SpinnerSelectedListener2.onItemSelected()...");
			Log.i(LOG_TAG,"arg2="+String.valueOf(arg2));

            if(arg2 == 10){
                cus_yearInvest.setVisibility(View.VISIBLE);
            }else{
                cus_yearInvest.setVisibility(View.INVISIBLE);
                yearInvest = Double.valueOf(invests[arg2]);
            }
		}
		public void onNothingSelected(AdapterView<?> arg0) {
			Log.i(LOG_TAG,"onNothingSelected()...");
		}
	}
	
	View.OnClickListener calculateListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
//            CalculatorUtil cal = new CalculatorUtil();
            if (cus_YearTarget.getVisibility() == View.VISIBLE) {

                if(TextUtils.isEmpty(cus_YearTarget.getText())){
                    ShowDialog("Warning","Year target must not be empty!");
                    return;
                }

                yearTarget = Integer.valueOf(String.valueOf(cus_YearTarget.getText()));

                if(yearTarget < 10){
                    ShowDialog("Warning","Year target must be greater than 10");
                    return;
                }
                if( yearTarget > 100){
                    ShowDialog("Warning","Year target must be less than 100");
                    return;
                }
            }
            if (cus_yearInvest.getVisibility() == View.VISIBLE ) {

                if(TextUtils.isEmpty(cus_yearInvest.getText())){
                    ShowDialog("Warning","Year invest must not be empty!");
                    return;
                }

                yearInvest = Double.valueOf(String.valueOf(cus_yearInvest.getText()));
                if(yearInvest <100000){
                    ShowDialog("Warning","Year Invest must be greater than 100,000");
                    return;
                }
                if(yearInvest > 1000000){
                    ShowDialog("Warning","Year Invest must be less than 100,000");
                    return;
                }
            }

            if(yearInvest<leastInvest || yearInvest> CalculatorUtil.MAX_YEAR_INVEST){
                ShowDialog("Warning","Year invest must in range( "+leastInvest+" - " + String.valueOf(CalculatorUtil.MAX_YEAR_INVEST)+" )!");
            }else{

                double yr = CalculatorUtil.getYearReturn(yearTarget, yearInvest);

                resultMsg.setVisibility(View.VISIBLE);
                resultMsg.setText(NumberUtil.formatData2Deci(yr * 100) + "%");
            }

        }
    };

    @Override
    protected void onDestroy() {
        if (adView != null) {
            adView.destroy();
        }
        super.onDestroy();
    }
	
/*	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}*/

}
